import json
import logging
import random
import time
import traceback
import pymysql
from config import Base_Path
def common(resp,exp_code):
    assert resp.status_code == 200
    assert exp_code == resp.json().get('code')
def common_search_goods(resp,exp_code,exp_total):
    assert resp.status_code == 200
    assert exp_code==resp.json().get("code")
    assert exp_total == resp.json().get('result').get('total')
def common_cart(resp,exp_code,exp_result):
    assert resp.status_code==200
    assert exp_code==resp.json().get('code')
    assert exp_result==resp.json().get('result')
# def common_order(resp,exp_code,exp_msg):
#     assert resp.status_code==200
#     assert exp_code==resp.json().get('code')
#     assert exp_msg in resp.json().get('msg')
def read_json(filename,apiname):
    file_path=Base_Path+'/data/'+filename+'.json'
    with open(file_path,encoding='utf-8')as f:
        data=json.load(f)
    api_data=data.get(apiname)
    print('获取到的数据为：{}'.format(api_data))
    cs_data=[]
    for i in api_data:
        cs_data.append(tuple(i.values()))
    print('测试数据为：{}'.format(cs_data))
    return cs_data
list1=[]
list2=[]
def read_csv(filename):
    file_path=Base_Path+'/data/'+filename+'.txt'
    with open(file_path,encoding='utf-8')as f:
        for i in f.readlines():
            list1.append(i.strip().replace('"', ""))
    for i in list1:
        list2.append(i.replace(',',''))
    print(list2)
def ordertoken():
    order_time=str(int(round(time.time()*1000)))
    random_digital = "0123456789zxcvbnmasdfghjklqwertyuiop"
    logging.info(order_time)
    print(order_time)
    num=''
    for i in range(8):
        number=random.randint(0,35)
        num+=random_digital[number]
    ordertoken=order_time+num
    logging.info('获取到的ordertoken为：{}'.format(ordertoken))
    return ordertoken
def exe_sql(sql):
    conn=None
    cur=None
    try:
        conn=pymysql.connect(host="47.114.106.48",password="mepsdevmysql",port=3306,user="root",database="tmotor-admin")
        cur=conn.cursor()
        cur.execute(sql)
        a=sql.split(' ')
        if 'select'==a[0].lower():
            return cur.fetchall()
        else:
            conn.commit()
            return f'受影响的行数为{cur.rowcount}'
    except:
        if conn is not None:
            conn.rollback()
        traceback.print_exc()
    finally:
        if cur is not None:
            cur.close()
        if conn is not None:
            conn.close()
if __name__ == '__main__':
    # read_json("qt","submitOrder_order_api")
    sql="SELECT * FROM customer_customer WHERE account='2524726232@qq.com'"
    a=exe_sql(sql)
    print(a)

